package com.bt.lms.ui.page.sys.page.paging;

import com.bt.lms.entity.page.Page;
import com.bt.lms.ui.page.sys.style.BtnStyle;
import com.bt.lms.ui.page.sys.style.LabStyle;
import com.bt.lms.ui.page.sys.utils.MyButton;
import com.bt.lms.ui.page.sys.utils.MyLabel;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;

import java.lang.reflect.Field;

/**
 * 分页模型
 *
 * @author 徐大永
 */
public class PagePaging extends HBox {

    /**
     * 跳转框
     */
    private TextField pageNumberTF;

    //-------------界面

    /**
     * 页码一号
     */
    private final MyButton pageOne = new MyButton();

    /**
     * 页码二号
     */
    private final MyButton pageTwo = new MyButton();

    /**
     * 页码三号
     */
    private final MyButton pageThree = new MyButton();

    /**
     * 按钮容器
     */
    private final HBox btnHBox = new HBox();

    /**
     * 当前使用者的分页模型
     */
    private final Page page;

    /**
     * 分页标签
     */
    public static final String FIRST_STR = "« 首页";

    /**
     * 分页标签
     */
    public static final String LAST_STR = "« 上一页";

    /**
     * 分页标签
     */
    public static final String NEXT_STR = "下一页 »";

    /**
     * 分页标签
     */
    public static final String FINALLY_STR = "尾页 »";

    //----------事件

    /**
     * 分页事件
     */
    private final EventHandler pageAction;

    //--------------数据

    /**
     * 总页数
     */
    private Long sumPage;

    /**
     * 总条数按钮
     */
    private Label sumLab;

    /**
     * 总页数标签
     */
    private Label sumPageLab;

    /**
     * 构建分页界面
     * 需要使用者内有:
     * 字段 page：获取当前页数，动态显示用.
     * 字段 pageAction：分页事件（此界面只给组件添加事件，并不实现事件内部）
     *
     * @param node  界面
     * @param clazz 界面的class
     */
    public PagePaging(Node node, Class clazz) {

        //1.获取当前使用者信息
        try {
            Field pageField = clazz.getDeclaredField("page");
            pageField.setAccessible(true);
            page = (Page) pageField.get(node);

            Field pageActionField = clazz.getDeclaredField("pageAction");
            pageActionField.setAccessible(true);
            pageAction = (EventHandler) pageActionField.get(node);

            //布局
            Insets insets = new Insets(10);
            this.setPadding(insets);
            this.setSpacing(10);
            this.setAlignment(Pos.CENTER_RIGHT);

            //2.初始化界面
            pageInit();
            //3.加载数据并渲染
            loadAndRenderPage();

        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);//获取类 Page失败
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);//未获取到字段 page
        }
    }

    /**
     * 界面初始化
     */
    private void pageInit() {

        int width = 45;


        MyLabel firstLab = new MyLabel(FIRST_STR);
        this.getChildren().add(firstLab);
        //样式
        LabStyle.paging(firstLab);
        //事件
        LabStyle.pagingAction(firstLab);
        firstLab.setOnMouseClicked(pageAction);

        MyLabel lastLab = new MyLabel(LAST_STR);
        this.getChildren().add(lastLab);
        //样式
        LabStyle.paging(lastLab);
        //事件
        LabStyle.pagingAction(lastLab);
        lastLab.setOnMouseClicked(pageAction);

        //中间控制按钮
        btnHBox.setSpacing(10);
        this.getChildren().add(btnHBox);

        MyLabel nextLab = new MyLabel(NEXT_STR);
        this.getChildren().add(nextLab);
        //样式
        LabStyle.paging(nextLab);
        //事件
        LabStyle.pagingAction(nextLab);
        nextLab.setOnMouseClicked(pageAction);

        MyLabel finallyLab = new MyLabel(FINALLY_STR);
        this.getChildren().add(finallyLab);
        //样式
        LabStyle.paging(finallyLab);
        //事件
        LabStyle.pagingAction(finallyLab);
        finallyLab.setOnMouseClicked(pageAction);

        //条数统计
        pagingSum(this);

        //跳转框
        pageNumberTF = new TextField();
        pageNumberTF.setPrefWidth(width);
        this.getChildren().add(pageNumberTF);
        //当前页标签
        String presentStr;
        if (page == null) {
            presentStr = "/" + 0;
        } else {
            presentStr = "/" + sumPage;
        }
        sumPageLab = new Label(presentStr);
        this.getChildren().add(sumPageLab);
        //跳转按钮
        MyButton notarizeBtn = new MyButton("GO");
        this.getChildren().add(notarizeBtn);
        //事件
        notarizeBtn.setOnAction(pageAction);

    }

    /**
     * 加载数据并渲染(分页按钮，总条数，总页数)
     * 分页按钮没有文字，调用前需要手动输入，
     * 否则界面可能无法正常显示
     */
    public void loadAndRenderPage() {

        //加载按钮
        if (getSumPage() != null) {
            if (getSumPage() >= 3) {
                //清空按钮容器数据
                btnHBox.getChildren().clear();
                btnHBox.getChildren().addAll(pageOne, pageTwo, pageThree);
                int current = page.getCurrent();//当前页
                if (current == getSumPage()) {//最后一页
                    pageOne.setText(String.valueOf(current - 2));
                    BtnStyle.paging(pageOne);
                    BtnStyle.pagingAction(pageOne);
                    pageTwo.setText(String.valueOf(current - 1));
                    BtnStyle.paging(pageTwo);
                    BtnStyle.pagingAction(pageTwo);
                    pageThree.setText(String.valueOf(current));
                    BtnStyle.pagingCurrent(pageThree);
                    BtnStyle.pagingCurrentAction(pageThree);
                    pageThree.setOnAction(pageAction);
                } else if (current == 1) {//第一页
                    pageOne.setText(String.valueOf(current));
                    BtnStyle.pagingCurrent(pageOne);
                    BtnStyle.pagingCurrentAction(pageOne);
                    pageOne.setOnAction(pageAction);
                    pageTwo.setText(String.valueOf(current + 1));
                    BtnStyle.paging(pageTwo);
                    BtnStyle.pagingAction(pageTwo);
                    pageThree.setText(String.valueOf(current + 2));
                    BtnStyle.paging(pageThree);
                    BtnStyle.pagingAction(pageThree);
                } else {//中间页
                    pageOne.setText(String.valueOf(current - 1));
                    BtnStyle.paging(pageOne);
                    BtnStyle.pagingAction(pageOne);
                    pageTwo.setText(String.valueOf(current));
                    BtnStyle.pagingCurrent(pageTwo);
                    BtnStyle.pagingCurrentAction(pageTwo);
                    pageTwo.setOnAction(pageAction);
                    pageThree.setText(String.valueOf(current + 1));
                    BtnStyle.paging(pageThree);
                    BtnStyle.pagingAction(pageThree);
                }
            } else if (getSumPage() == 2) {
                //清空按钮容器数据
                btnHBox.getChildren().clear();
                btnHBox.getChildren().addAll(pageOne, pageTwo);
                int current = page.getCurrent();//当前页
                if (current == 2) {//最后一页
                    pageOne.setText(String.valueOf(current - 1));
                    BtnStyle.paging(pageOne);
                    BtnStyle.pagingAction(pageOne);
                    pageTwo.setText(String.valueOf(current));
                    BtnStyle.pagingCurrent(pageTwo);
                    BtnStyle.pagingCurrentAction(pageTwo);
                    pageTwo.setOnAction(pageAction);
                } else if (current == 1) {//第一页
                    pageOne.setText(String.valueOf(current));
                    BtnStyle.pagingCurrent(pageOne);
                    BtnStyle.pagingCurrentAction(pageOne);
                    pageOne.setOnAction(pageAction);
                    pageTwo.setText(String.valueOf(current + 1));
                    BtnStyle.paging(pageTwo);
                    BtnStyle.pagingAction(pageTwo);
                }
            } else if (getSumPage() == 1) {
                //清空按钮容器数据
                btnHBox.getChildren().clear();
                btnHBox.getChildren().addAll(pageOne);
                pageOne.setText("1");
                BtnStyle.pagingCurrent(pageOne);
                BtnStyle.pagingCurrentAction(pageOne);
                pageOne.setOnAction(pageAction);
            }
        }

        //条数统计重新加载
        try {
            sumLab.setText(String.valueOf(page.getTotal()));
        } catch (Exception e) {
            hint("未获取到总条数，请检查是否存在总条数。");
        }

        //页数统计重新加载
        sumPageLab.setText(String.valueOf(getSumPage()));
    }


    /**
     * 分页总和方法
     * 填写分页总和标签
     *
     * @param searchHBox 分页盒子
     */
    private void pagingSum(HBox searchHBox) {

        Label explainLab = new Label("总记录数");
        searchHBox.getChildren().add(explainLab);

        sumLab = new Label(String.valueOf(sumPage));
        searchHBox.getChildren().add(sumLab);
        //样式
        LabStyle.status(sumLab, LabStyle.STOP);

        Label unitLab = new Label("条");
        searchHBox.getChildren().add(unitLab);

    }

    /**
     * 提示
     * 以弹窗的形式
     *
     * @param hintStr 提示文本
     */
    private void hint(String hintStr) {

        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("错误");
        alert.setHeaderText(null);
        alert.setContentText(hintStr);
        alert.showAndWait();

    }

    public TextField getPageNumberTF() {
        return pageNumberTF;
    }

    public Long getSumPage() {
        return sumPage;
    }

    public void setSumPage(Long sumPage) {
        this.sumPage = sumPage;
    }

    public MyButton getPageOne() {
        return pageOne;
    }

    public MyButton getPageTwo() {
        return pageTwo;
    }

    public MyButton getPageThree() {
        return pageThree;
    }
}